﻿using System;
using System.Collections.Generic;
using System.Text;
using RuoTeng.Util;
using RTGather.Lib.gather;

namespace RTGather.Lib
{
   public class GatherTask:Task
    {
                                                         
        private string filename = "";

        public string keyword = "";

         

        public delegate void LogHandle(string text,System.Drawing.Color color);

        public event LogHandle Log;

        public string type = "";


        //public delegate void PrintVisitorHandle(List<string> visistors);

      //  public event PrintVisitorHandle PrintVisitor;

        public GatherTask(string filename)
        {
             this.filename = filename;                     
        }

        protected override void OnStart()
        {
            base.OnStart();
             
            fbb_bugamazon f = new fbb_bugamazon();
            int Count = f.getCount();
            int pagenum = Count; 
            //-------------------------------------------------------------------------------------------------
            int i = 0;
            for (int y = 1; y <= pagenum; y++)
            {
                string url = f.getUrl(y);
              
                f.getData(url);
                i++;
                string result="";
                foreach (Product c in f.clist)
                {
                    result += c.Name + System.Environment.NewLine;
                }
                CallMessage(result, System.Drawing.Color.Green);
               
            }

          
            this.Stop();
            //-------------------------------------------------------------------------------------------------
            if (this.ToChangeState == RuoTeng.Util.Task.TaskState.Pause)
            {
                this.OnPause();
            }
            else if (this.ToChangeState == RuoTeng.Util.Task.TaskState.Stop)
            {
                this.OnStop();
            }
        }

        protected override void OnPause()
        {
            base.OnPause();
            this.CallMessage("任务暂停！", System.Drawing.Color.Red);
        }

        protected override void OnStop()
        {           
            base.OnStop();   
        }

        private void CallMessage(string message,System.Drawing.Color color)
        {
            if (this.Log != null)
            {
               this.Log(message, color);
            }
        }

        
    }
}
